<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="项目名称" prop="problemProject">
        <el-input v-model="queryParams.problemProject" placeholder="请输入项目名称" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="创建人" prop="createUserName">
        <el-input v-model="queryParams.createUserName" placeholder="请输入创建人" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['system:records:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['system:records:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['system:records:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['system:records:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row> -->

    <el-table v-loading="loading" :data="recordsList">

      <!-- <el-table-column label="问题处理记录id" align="center" prop="id" /> -->

      <el-table-column label="项目名称" align="center" prop="problemProject" />
      <el-table-column label="创建人" align="center" prop="createUserName" />

      <el-table-column label="问题描述" align="center" prop="problemDescription" :show-overflow-tooltip="true"
        min-width="200" />
      <el-table-column label="解决方案" align="center" prop="scheme" :show-overflow-tooltip="true" min-width="200" />
      <el-table-column label="状态" align="center" prop="progressNode">
        <template slot-scope="scope" v-if="JSON.stringify(statusList) !== '{}'">
          <el-tag :color="tagType[scope.row.progressNode]" style="color: #fff;">
            {{ statusList[scope.row.progressNode] }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="问题类型" align="center" prop="dictCode">
        <template slot-scope="scope" v-if="JSON.stringify(statusList) !== '{}'">
          <div class="type">
            {{ typeList[scope.row.dictCode] }}
          </div>

        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['system:records:remove']">实施完成</el-button>
        </template>
      </el-table-column>
    </el-table>


    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改问题记录对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body :close-on-click-modal="false">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="审批意见" prop="auditOpinion">
          <el-input v-model="form.auditOpinion" type="textarea" :autosize="{ minRows: 4, maxRows: 4 }"
            placeholder="请输入审批意见" />
        </el-form-item>
        <el-form-item label="处理人" prop="examineUserId">
          <el-select v-model="form.examineUserId" placeholder="请选择处理人" clearable :style="{ width: '100%' }">
            <el-option v-for="(item, index) in userList" :key="index" :label="item.label"
              :value="item.value"></el-option>
          </el-select>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm(1)">同意</el-button>
        <el-button @click="submitForm(2)">驳回</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listRecords, getRecords, delRecords, addRecords, updateRecords } from "@/api/system/records";
import { listData } from "@/api/system/dict/data";

export default {
  name: "Records",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      tagType: {
        progress_node_1: '#909399',
        progress_node_2: '#409eff',
        progress_node_3: '#f56c6c',
        progress_node_4: '#a0cfff',
        progress_node_5: '#e6a23c',
        progress_node_6: '#67c23a',
      },
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 问题记录表格数据
      recordsList: [],
      userList: this.$store.getters.user_list,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 问题类型列表
      typeList: [],
      // 状态字典
      statusList: {},
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        problemProject: null,
        createUserName: null,
        progressNode: 'progress_node_5',
        examineUserId: this.$store.getters.user_id

      },
      // 表单参数
      form: {
        problemDescription: ''
      },
      // 表单校验
      rules: {
        auditOpinion: [
          { required: true, message: "审批意见不能为空", trigger: "blur" }
        ],
        examineUserId: [{
          required: true,
          message: '请选择处理人',
          trigger: 'change'
        }
        ],


      }
    };
  },
  created() {
    this.init()

  },
  methods: {

    async init() {
      await this.getStatus('progress_node', this.statusList)
      await this.getStatus('classification_of_security_issues', this.typeList)
      await this.getList();

    },

    /** 查询问题记录列表 */
    async getList() {
      this.loading = true;
      return listRecords(this.queryParams).then(response => {
        this.recordsList = response.rows
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        problemProject: null, // 项目
        problemDescription: null, // 描述
        progressNode: null, // 问题状态

      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加问题记录";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getRecords(id).then(response => {
        this.form = response.data;
        this.$confirm('确认此方案是否实施完成?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'success'
        }).then(() => {
          this.form.progressNode = "progress_node_6"
          updateRecords(this.form).then(response => {
            this.$modal.msgSuccess('实施完成')
            this.open = false;
            this.getList();
          });
        })
      });
    },
    // 获取状态字典
    async getStatus(name, list) {
      let params = {
        pageNum: 1,
        pageSize: 100,
        dictType: name
      }
      return listData(params).then(res => {
        let list1 = []
        list1 = res.rows
        list1.forEach(item => {
          list[item.dictValue] = item.dictLabel
        })

      })
    },


    /** 提交按钮 */
    submitForm(num) {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {

            this.form.progressNode = num === 1 ? "progress_node_5" : "progress_node_3"
            updateRecords(this.form).then(response => {
              let msg = num === 1 ? '审批成功' : '已驳回'
              this.$modal.msgSuccess(msg)
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除问题记录编号为"' + ids + '"的数据项？').then(function () {
        return delRecords(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/records/export', {
        ...this.queryParams
      }, `records_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
<style scoped>
.type {
  font-weight: bold;
}
</style>